<template>
  <div>
    <r-search :searchHandle="searchHandleAdd" />
    <r-table
      :isSelection="false"
      :isIndex="false"
      :isPagination="true"
      :tableData="tableData"
      :tableCols="tableCols"
      :tablePage="tablePage"
      @refresh="init()"
    ></r-table>
    <r-form
      :formRules="formRules"
      :formHandle="formHandle"
      :formData="formData"
      :formCols="formCols"
      ref="elForm"
      labelWidth="120px"
      dialogWidth="700px"
      :isHandle="true"
    />
  </div>
</template>
<script>
export default {
  data() {
    var checkpass2 = (rule, value, callback) => {
      if (value != this.formDataEditPsw.password) {
        callback(new Error("The password is not the same!"));
      } else {
        callback();
      }
    };
    return {
      searchHandleAdd: [
        {
          label: "Bind Add User",
          type: "primary",
          plain: true,
          icon: "el-icon-circle-plus-outline",
          handle: e => this.elFormVisible()
        }
      ],

      tableData: [],
      tableCols: [
        // {
        //   label: '姓名',
        //   prop: 'trueName'
        // },
        {
          label: "User Name",
          prop: "nickName"
        },
        {
          label: "Phone",
          prop: "phone"
        },
        {
          label: " User Type",
          prop: "userType",
          itemClass: e =>
            e.userType == "2" ? "danger" : e.userType == "1" ? "success" : "--",
          formatter: e =>
            e.userType === "1" ? "WeChat" : e.userType === "2" ? "Alipay" : "--"
        },

        {
          label: "Operations",
          type: "button",
          width: 230,
          btnList: [
            {
              label: "Delete",
              type: "danger",
              icon: "el-icon-delete",
              plain: true,
              handle: row => {
                this.$confirm("Confirm Delete?", "Hint", {
                  confirmButtonText: "Confirm",
                  cancelButtonText: "Cancel",
                  type: "warning"
                })
                  .then(async () => {
                    var res = await this.apis.userUnBindUserByUserId(
                      row.userId
                    );
                    if (res.code === 200) {
                      this.$message({
                        type: "success",
                        message: "Delete Success!"
                      });
                    } else {
                      this.$message({
                        type: "danger",
                        message: "Delete Failure"
                      });
                    }

                    this.init();
                  })
                  .catch(() => {
                    this.$message({
                      type: "primary",
                      message: "Already Cancel"
                    });
                  });
              }
            }
          ]
        }
      ],
      tablePage: { limit: 10, page: 1, total: 0 },
      formData: {
        phone: ""
      },
      formCols: [
        {
          type: "input",
          label: "Phone",
          placeholder:
            "Please enter the phone number of the user you want to bind",
          width: 450,
          prop: "phone"
        }
      ],
      formRules: {
        phone: [{ required: true, message: "Please Enter", trigger: "blur" }]
      },
      formHandle: [
        {
          label: "Cancel",
          handle: e => this.elFormVisible()
        },
        {
          label: "Confirm",
          type: "success",
          handle: e => this.elFormSubmit()
        }
      ]
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    async init() {
      let res = await this.apis.userGetBindAppUserList();
      if (res) {
        this.tablePage.total = res.total;
        this.tableData = res.data || [];
      }
    },
    // Edit按钮里面---确认按钮
    async elFormSubmit() {
      this.$refs.elForm.$refs.ruleForm.validate(async valid => {
        if (valid) {
          const res = await this.apis.userBindAppUser(this.formData.phone);
          this.$message({
            type: res.code == 200 ? "success" : "error",
            message: res.msg
          });
          this.elFormVisible();
          this.init();
        }
      });
    }
  }
};
</script>
<style scoped lang="scss">
.el-icon-search {
  margin-right: 10px;
}

.Tip {
  margin-left: 20px;
}
</style>
